<template>
  <div>
    <el-divider></el-divider>
    <div style="font-size:20px;text-align:left">
      <i class="el-icon-document-checked">窗口挂号</i>
    </div>
    <el-divider></el-divider>
    <div style="text-align:left">
      <el-button type="primary" @click="addNewRegist()">挂号</el-button>
      <el-button type="primary" @click="clearRegist()">清空</el-button>
    </div>

    <el-descriptions title="基本信息"  :column="3" border style="margin-top:20px">
      <el-descriptions-item label="病历号">
        <el-input v-model="register.case_number" placeholder="请输入内容"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="姓名">
        <el-input v-model="register.real_name" placeholder="请输入姓名"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="性别">
        <el-select v-model="register.gender" placeholder="请选择">
          <el-option v-for="item in register_gender"  :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="年龄">
        <template>
          <el-input v-model="register.age" placeholder="请输入年龄" style="width:100px"></el-input>
          <el-select v-model="register.age_type" placeholder="请选择" style="width:80px">
            <el-option v-for="item in register_ageType"  :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-descriptions-item>
      <el-descriptions-item label="出生日期">
        <el-date-picker v-model="register.birthday" type="date"  placeholder="选择日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-descriptions-item>
      <el-descriptions-item label="身份证号">
        <el-input v-model="register.card_number" placeholder="请输入身份证号"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="家庭住址">
        <el-input v-model="register.home_address" placeholder="请输入家庭住址（可选）"></el-input>
      </el-descriptions-item>
    </el-descriptions>

    <el-descriptions title="挂号信息" :column="3" border style="margin-top:20px">
      <el-descriptions-item label="结算类别">
        <el-select v-model="register.settle_id" placeholder="请选择">
          <el-option v-for="item in register_settle"  :key="item.id" :label="item.settle_name" :value="item.id">
          </el-option>
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="看诊日期" >
        <el-date-picker disabled v-model="register.visit_date" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-descriptions-item>
      <el-descriptions-item label="午别" >
        <el-select disabled v-model="register.noon" placeholder="请选择">
          <el-option v-for="item in register_noon"  :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="挂号科室">
        <el-select v-model="register.deptment_id" placeholder="请选择">
          <el-option v-for="item in register_dept"  :key="item.id" :label="item.dept_name" :value="item.id">
          </el-option>
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="号别">
        <el-select v-model="register.regist_level_id" placeholder="请选择" @change="registerLevelChange">
          <el-option v-for="item in register_level"  :key="item.id" :label="item.regist_name" :value="item.id">
          </el-option>
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="看诊医生">
        <el-select v-model="register.employee_id" placeholder="请选择" @change="getAlreadyRegisterCount">
          <el-option v-for="item in register_employee"  :key="item.id" :label="item.realname" :value="item.id">
          </el-option>
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="初始号额" >
        <el-input disabled v-model="register.total_number" placeholder="初始号额"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="已用号额">
        <el-input disabled v-model="register.used_number" placeholder="已用号额"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="病历本">
        <el-switch v-model="register.is_book" active-color="#13ce66" inactive-color="#ff4949" @change="registerIsbookChange">
        </el-switch>
      </el-descriptions-item>
      <el-descriptions-item label="应收金额">
        <el-input disabled v-model="register.regist_money" placeholder="请输入金额"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="收费方式">
        <el-select v-model="register.regist_method" placeholder="请选择">
          <el-option v-for="item in register_method"  :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-descriptions-item>
    </el-descriptions>
  </div>
</template>
<script>
  import qs from 'qs'
  
  export default {
    methods:{
      //添加挂号信息到后端
      addNewRegist(){
        try{
          parseInt(this.register.case_number);
        }catch{
          this.$message({
            message:"病例号必须为数字",
            type:"error"
          });
          return;
        }
        
        this.$http.post("http://localhost:8092/addRegister",qs.stringify(this.register)).then(
          (resp)=>{
            this.$message({message:resp.data,type:'success'});
            this.clearRegist();
          }
        )        
      },
      //已经挂号数量统计
      getAlreadyRegisterCount(){
        this.$http.get("http://localhost:8092/getAlreadyRegisterCount?"
            +"visitDate="+this.register.visit_date
            +"&noon="+this.register.noon
            +"&employeeId="+this.register.employee_id
          ).then(
          (resp)=>{
            this.register.used_number = resp.data;
            if(this.register.used_number == this.register.total_number){
              this.$message({
                message: '警告，该医生号额已满，请选择其他医生',
                type: 'warning'
              });
            }
          }
        )
      },
      //清空已经填写的挂号信息
      clearRegist(){
        this.register.id = '';
        this.register.case_number = '';
        this.register.real_name = '';
        this.register.gender = '男';
        this.register.card_number = '';
        this.register.birthday = '';
        this.register.age = '';
        this.register.age_type = '年';
        this.register.home_address = '';
        this.initDateTime();
        this.register.settle_id = '';
        this.register.deptment_id = '';
        this.register.employee_id = '';
        this.register.regist_level_id = '';
        this.register.settle_category_id = '1';
        this.register.is_book = false;
        this.register.used_number='0';
        this.register.regist_method = '医保卡';
        this.register.regist_money = '';
        this.register.visit_state = '';
      },
      //选择不同的号别
      registerLevelChange(noonValue){
        this.register.is_book = false;
        for( let item of this.register_level){
          if(noonValue == item.sequence_no){
            this.register.regist_money = item.regist_fee;
            this.register.total_number = item.regist_quota;
          }
        }
        
        //得到科室id和号别的id
        // console.log("科室id："+this.register.deptId);
        // console.log("号别id："+this.register.registLeId)
        this.register.employee_id = '';
        this.$http.get("http://localhost:8092/getRegistDoctorList?"
            +"deptment_id="+this.register.deptment_id
            +"&regist_level_id="+this.register.regist_level_id).then(
          (resp)=>{
            this.register_employee = resp.data;
          }
        )
      },
      //是否选择购买病历本
      registerIsbookChange(){
        if(this.register.is_book){
          this.register.regist_money = this.register.regist_money + 1;
        }else{
          this.register.regist_money = this.register.regist_money - 1;
        }
      },
      //得到挂号级别
      getRegistLevelList(){
        this.$http.get("http://localhost:8092/getRegistLevelList").then(
          (resp)=>{
            this.register_level = resp.data;
          }
        )
      },
      //得到部门信息
      getDeptList(){
        this.$http.get("http://localhost:8092/getAllDeptList").then(
          (resp)=>{
            this.register_dept = resp.data;
          }
        )
      },
      //得到结算类别
      getSettleCategory(){
        this.$http.get("http://localhost:8092/getSettleCategory").then(
          (resp)=>{
            this.register_settle = resp.data;
          }
        )
      },
      //得到当前时间
      initDateTime(){
        //获取当前时间
        let nowDate = new Date();

        let nowDateString = nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+"-"+nowDate.getDate()
        this.register.visit_date = nowDateString;
        //获取当前的小时
        let nowHours = nowDate.getHours();
        if(nowHours<=12){
          this.register.noon = '上午';
        }else{
          this.register.noon = '下午';
        }
      },
      //得到最大病例号
      getMaxCaseNumber(){
        this.$http.get("http://localhost:8092/getMaxCaseNumber").then(
          (resp)=>{
            this.register.case_number = parseInt(resp.data) + 1;
          }
        )        
      },
      
    },
    mounted:function(){
      this.initDateTime();
      //得到挂号级别
      this.getRegistLevelList();
      //得到部门信息
      this.getDeptList();
      //得到结算类别
      this.getSettleCategory();
      //得到最大病例号
      this.getMaxCaseNumber();

    },
    data() {
      return {
        register:{
          id:'',
          case_number:'',//病历号
          real_name:'',//姓名
          gender:'男',//性别
          card_number:'',//身份证
          birthday:'',//出生日期
          age:'',//年龄
          age_type:'年',//年龄类型
          home_address:'',//家庭住址          
          visit_date:'',//看诊日期
          noon:'上午',//午别
          deptment_id:'',//科室编号
          employee_id:'',//医生编号
          regist_level_id:'',//挂号级别编号
          settle_category_id:'1',//结算类别编号
          used_number:'0',
          is_book:false,//病历本
          regist_method:'医保卡',//收费方式
          regist_money:'',//收费金额
          visit_state:''//看诊状态
        },
        register_gender:[
            {
              value:'男',
              label:'男'
            },
            {
              value:'女',
              label:'女'
            }
        ],
        register_ageType:[
          {
            value:'年',
            label:'年'
          },
          {
            value:'天',
            label:'天'
          }
        ],
        register_method:[
          {
            value:'现金',
            label:'现金'
          },
          {
            value:'微信',
            label:'微信'
          },
          {
            value:'支付宝',
            label:'支付宝'
          },
          {
            value:'银行卡',
            label:'银行卡'
          },
          {
            value:'医保卡',
            label:'医保卡'
          }
        ],
        register_level:[],
        register_employee:[],
        register_noon:[
          {
            value:'上午',
            label:'上午'
          },
          {
            value:'下午',
            label:'下午'
          }
        ],
        register_settle:[],
        register_dept:[],
      }
    }
  }
</script>